#coding:utf-8
import hashlib
import time
import urllib
from tkinter import *
from qm_interface import *
from xml_json import *

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

LOG_LINE_NUM = 0

class InterGui():

	def __init__(self, init_window_name):
		self.init_window_name = init_window_name


	def set_init_win(self):
		self.init_window_name.title("Interface testing tool")
		self.init_window_name.geometry("1000x600+10+10")
		#self.init_window_name["bg"] = "pink"
		#self.init_window_name.attributes("-alpha",0.9)

		#标签
		self.init_data_label = Label(self.init_window_name, text="待处理数据")
		self.init_data_label.grid(row=0, column=0)
		self.result_data_label = Label(self.init_window_name, text="输出结果")
		self.result_data_label.grid(row=0, column=12)
		self.log_label = Label(self.init_window_name, text="日志")
		self.log_label.grid(row=12, column=0)

        #文本框
		self.init_data_Text = Text(self.init_window_name, width=67, height=35)  #原始数据录入框
		self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)

		self.result_data_Text = Text(self.init_window_name, width=70, height=49)  #处理结果展示
		self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10) 

		self.log_data_Text = Text(self.init_window_name, width=66, height=9)  # 日志框
		self.log_data_Text.grid(row=13, column=0, columnspan=10)
 


		#MD5按钮
		self.str_trans_to_md5_button = Button(self.init_window_name, text="字符串转MD5", bg="lightblue", width=10, command=self.str_trans_to_md5)  # 调用内部方法  加()为直接调用
		self.str_trans_to_md5_button.grid(row=1, column=11)

		#url解码
		self.url_decoding_button = Button(self.init_window_name, text="url_decoding", bg="lightblue", width=10, command=self.url_decoding)  # 调用内部方法  加()为直接调用
		self.url_decoding_button.grid(row=2, column=11)

		#url编码
		self.url_encoded_button = Button(self.init_window_name, text="url_encoded", bg="lightblue", width=10, command=self.url_encoded)  # 调用内部方法  加()为直接调用
		self.url_encoded_button.grid(row=3, column=11)

		#qm_order_cre_int发货单创建接口
		self.qm_order_cre_int_button = Button(self.init_window_name, text="奇门接口测试", bg="lightblue", width=10, command=self.qm_order_cre_int)  # 调用内部方法  加()为直接调用
		self.qm_order_cre_int_button.grid(row=4, column=11)

		#xml_json xml_TO_json
		self.xml_TO_json_button = Button(self.init_window_name, text="xml_TO_json", bg="lightblue", width=10, command=self.xml_TO_json)  # 调用内部方法  加()为直接调用
		self.xml_TO_json_button.grid(row=5, column=11)

	#功能函数
	def str_trans_to_md5(self):
		src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
        #print("src =",src)
		if src:
			try:
				myMd5 = hashlib.md5()
				myMd5.update(src)
				myMd5_Digest = myMd5.hexdigest()
				#print(myMd5_Digest)
				#输出到界面
				self.result_data_Text.delete(1.0,END)
				self.result_data_Text.insert(1.0,myMd5_Digest)
				self.write_log_to_Text("INFO:str_trans_to_md5 success")
			except:
				self.result_data_Text.delete(1.0,END)
				self.result_data_Text.insert(1.0,"字符串转MD5失败")
		else:
			self.write_log_to_Text("error: Input is null")


	def url_decoding(self):
		#url解码
		src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
		if src:
			result = urllib.unquote(str(src))

			self.result_data_Text.delete(1.0,END)
			self.result_data_Text.insert(1.0,result)
			self.write_log_to_Text("INFO:url_decoding success")
		else:
			print "Error"

	def url_encoded(self):
		#url编码
		src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
		if src:
			result = urllib.quote(str(src))

			self.result_data_Text.delete(1.0,END)
			self.result_data_Text.insert(1.0,result)
			self.write_log_to_Text("INFO:url_encoded success")
		else:
			print "Error"

	#result = Xml2Json(xml).result 

	def xml_TO_json(self):
		src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
		if src:
			result = json.dumps(Xml2Json(str(src)).result,sort_keys=True, indent=4)
			self.result_data_Text.delete(1.0,END)
			self.result_data_Text.insert(1.0,result)
			self.write_log_to_Text("INFO:xml_TO_json success")
		else:
			print "Error"



	def qm_order_cre_int(self):
		# qm_interface("deliveryorder.create", other_content=other_content)

		src = self.init_data_Text.get(1.0,END).strip().encode()
		order_type = str(src).split(",")
		print "order_type",order_type

		if src:
			#发货单创建
			if src == "deliveryorder.create":
				result = qm_interface("deliveryorder.create", orderType='jyck', type="json")

			#出库单
			elif order_type[0] == "stockout.create":
				result = qm_interface("stockout.create", orderType=order_type[1].strip().upper(), type="json")

			#入库单
			elif src == "entryorder.create":
				result = qm_interface("entryorder.create", orderType="CGRK", type="xml")

			#商品同步
			elif src == "singleitem.synchronize":
				result = qm_interface("singleitem.synchronize", orderType="add", type="xml")

			else:
				self.write_log_to_Text("error: method has not found")
				return None

			self.result_data_Text.delete(1.0,END)
			self.result_data_Text.insert(1.0,result)
			self.write_log_to_Text("INFO:qm_order_cre_int success")

		else:
			self.write_log_to_Text("Error: input error")

	#获取当前时间
	def get_current_time(self):
		current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
		return current_time


    #日志动态打印
	def write_log_to_Text(self,logmsg):
		global LOG_LINE_NUM
		current_time = self.get_current_time()
		logmsg_in = str(current_time) +" " + str(logmsg) + "\n"
		if LOG_LINE_NUM <= 7:
			self.log_data_Text.insert(END, logmsg_in)
			LOG_LINE_NUM = LOG_LINE_NUM + 1
		else:
			self.log_data_Text.delete(1.0,2.0)
			self.log_data_Text.insert(END, logmsg_in)



def gui_start():
	init_window = Tk()
	init_window_por = InterGui(init_window)
	init_window_por.set_init_win()
	init_window.mainloop()

gui_start()




